home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / ghostscript / src / idebug.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  7KB  |  240 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* idebug.c */
  20. /* Debugging support for Ghostscript interpreter */
  21. #include "string_.h"
  22. #include "ghost.h"
  23. #include "dict.h"
  24. #include "iname.h"
  25. #include "iutil.h"
  26. #include "ostack.h"            /* for opdef.h */
  27. #include "opdef.h"
  28. #include "packed.h"
  29. #include "store.h"        /* for make_oper for opdef.h */
  30.  
  31. /* Forward references */
  32. void debug_print_string(P2(const byte *, ushort));
  33. void debug_print_ref(P1(const ref *));
  34.  
  35. #define debug_print_name(pnref)\
  36.   debug_print_string((pnref)->value.pname->string_bytes,\
  37.              (pnref)->value.pname->string_size)
  38.  
  39. /* Table of type name strings */
  40. static const char *type_strings[] = { type_print_strings };
  41.  
  42. /* Print a ref */
  43. void
  44. debug_print_full_ref(const ref *pref)
  45. {    unsigned size = r_size(pref);
  46.     ref nref;
  47.     dprintf1("(%x)", r_type_attrs(pref));
  48.     switch ( r_type(pref) )
  49.        {
  50.     case t_array:
  51.       dprintf2("array(%u)0x%lx", size, (ulong)pref->value.refs); break;
  52.     case t_boolean:
  53.       dprintf1("boolean %x", pref->value.index); break;
  54.     case t_condition:
  55.       dprintf1("condition 0x%lx", (ulong)pref->value.pcond); break;
  56.     case t_device:
  57.       dprintf1("device 0x%lx", (ulong)pref->value.pdevice); break;
  58.     case t_dictionary:
  59.       dprintf3("dict(%u/%u)0x%lx",
  60.            dict_length(pref), dict_maxlength(pref),
  61.            (ulong)pref->value.pdict);
  62.       break;
  63.     case t_file:
  64.       dprintf1("file 0x%lx", (ulong)pref->value.pfile); break;
  65.     case t_gstate:
  66.       dprintf1("gstate 0x%lx", (ulong)pref->value.pgstate); break;
  67.     case t_integer: dprintf1("int %ld", pref->value.intval); break;
  68.     case t_lock:
  69.       dprintf1("lock 0x%lx", (ulong)pref->value.plock); break;
  70.     case t_mark: dprintf("mark"); break;
  71.     case t_mixedarray:
  72.       dprintf2("mixed packedarray(%u)0x%lx", size,
  73.            (ulong)pref->value.packed); break;
  74.     case t_name:
  75.       dprintf2("name(0x%lx#%x)", (ulong)pref->value.pname,
  76.            name_index(pref));
  77.       debug_print_name(pref);
  78.       break;
  79.     case t_null: dprintf("null"); break;
  80.     case t_oparray:
  81.       dprintf1("op_array(0x%x)", size);
  82.       name_index_ref(op_array_nx_table[size - op_def_count], &nref);
  83.       debug_print_name(&nref);
  84.       break;
  85.     case t_operator:
  86.       dprintf1("op(0x%x", size);
  87.       if ( size )
  88.         dprintf1(":%s", (const char *)(op_def_table[size]->oname + 1));
  89.       dprintf1(")0x%lx", (ulong)pref->value.opproc);
  90.       break;
  91.     case t_real: dprintf1("real %f", pref->value.realval); break;
  92.     case t_shortarray:
  93.       dprintf2("short packedarray(%u)0x%lx", size,
  94.            (ulong)pref->value.packed); break;
  95.     case t_string:
  96.       dprintf2("string(%u)0x%lx", size, (ulong)pref->value.bytes); break;
  97.     default: dprintf1("type 0x%x", r_type(pref));
  98.        }
  99. }
  100. void
  101. debug_print_packed_ref(const ref_packed *pref)
  102. {    ushort elt = *pref;
  103.     ref nref;
  104.     switch ( elt >> packed_type_shift )
  105.        {
  106.     case pt_executable_operator:
  107.       dprintf("<op_name>");
  108.       elt &= packed_int_mask;
  109.       op_index_ref(elt, &nref);
  110.       debug_print_ref(&nref);
  111.       break;
  112.     case pt_integer:
  113.       dprintf1("<int> %d", (int)(elt & packed_int_mask) + packed_min_intval);
  114.       break;
  115.     case pt_literal_name: case pt_literal_name+1:
  116.       dprintf("<lit_name>"); elt &= packed_max_name_index; goto ptn;
  117.     case pt_executable_name: case pt_executable_name+1:
  118.       dprintf("<exec_name>"); elt &= packed_max_name_index;
  119. ptn:      name_index_ref(elt, &nref);
  120.       dprintf2("(0x%lx#%x)", (ulong)nref.value.pname, elt);
  121.       debug_print_name(&nref);
  122.       break;
  123.        }
  124. }
  125. void
  126. debug_print_ref(const ref *pref)
  127. {    if ( r_is_packed(pref) )
  128.         debug_print_packed_ref((const ref_packed *)pref);
  129.     else
  130.         debug_print_full_ref(pref);
  131.     fflush(dstderr);
  132. }
  133.  
  134. /* Print a string */
  135. void
  136. debug_print_string(const byte *chrs, ushort len)
  137. {    ushort i;
  138.     for ( i = 0; i < len; i++ )
  139.         dputc(chrs[i]);
  140.     fflush(dstderr);
  141. }
  142.  
  143. /* Dump one ref */
  144. void
  145. debug_dump_one_ref(const ref *p)
  146. {    uint attrs = r_type_attrs(p);
  147.     uint type = r_type(p);
  148.     static const char *as = attr_print_string;
  149.     const char *ap = as;
  150. #define buf_size 30
  151.     char buf[buf_size + 1];
  152.     uint plen;
  153.     if ( type >= tx_next_index )
  154.         dprintf1("0x%02x?? ", type);
  155.     else if ( type >= t_next_index )
  156.         dprintf("opr* ");
  157.     else
  158.         dprintf1("%s ", type_strings[type]);
  159.     for ( ; *ap; ap++, attrs >>= 1 )
  160.       if ( *ap != '.' )
  161.         dputc(((attrs & 1) ? *ap : '-'));
  162.     dprintf2(" 0x%04x 0x%08lx", r_size(p), *(const ulong *)&p->value);
  163.     if ( obj_cvs(p, (byte *)buf, countof(buf) - 1, &plen) >= 0 &&
  164.          ((buf[plen] = 0), strcmp(buf, "--nostringval--"))
  165.        )
  166.         dprintf1(" = %s", buf);
  167.     fflush(dstderr);
  168. }
  169.  
  170. /* Dump a region of memory containing refs */
  171. void
  172. debug_dump_refs(const ref *from, uint size, const char *msg)
  173. {    const ref *p = from;
  174.     uint count = size;
  175.     if ( size && msg )
  176.         dprintf2("%s at 0x%lx:\n", msg, (ulong)from);
  177.     while ( count-- )
  178.        {    dprintf2("..%04x: 0x%02x ", (uint)p & 0xffff, r_type(p));
  179.         debug_dump_one_ref(p);
  180.         dputc('\n');
  181.         p++;
  182.        }
  183. }
  184.  
  185. /* Dump a region of memory */
  186. void
  187. debug_dump_bytes(const byte *from, const byte *to, const char *msg)
  188. {    const byte *p = from;
  189.     if ( from < to && msg )
  190.         dprintf1("%s:\n", msg);
  191.     while ( p != to )
  192.        {    const byte *q = min(p + 16, to);
  193.         dprintf1("%lx:", (ulong)p);
  194.         while ( p != q ) dprintf1(" %02x", *p++);
  195.         dputc('\n');
  196.        }
  197. }
  198.  
  199. /* Dump an array. */
  200. void
  201. debug_dump_array(const ref *array)
  202. {    const ref_packed *pp;
  203.     unsigned int type = r_type(array);
  204.     uint len;
  205.  
  206.     switch (type)
  207.        {
  208.     default:
  209.         dprintf2 ("%s at 0x%lx isn't an array.\n",
  210.               (type < countof(type_strings) ?
  211.                type_strings[type] : "????"),
  212.               (ulong)array);
  213.         return;
  214.     case t_oparray:
  215.         /* This isn't really an array, but we'd like to see */
  216.         /* its contents anyway. */
  217.         debug_dump_array(op_array_table.value.refs + op_index(array) -
  218.                  op_def_count);
  219.         return;
  220.     case t_array:
  221.     case t_mixedarray:
  222.     case t_shortarray: 
  223.         ;
  224.        }
  225.  
  226.     /* This "packed" loop works for all array-types. */
  227.     for ( len = r_size (array), pp = array->value.packed;
  228.           len > 0;
  229.           len--, pp = packed_next(pp))
  230.        {    ref temp;
  231.         packed_get(pp, &temp);
  232.         dprintf3("..%04x%c 0x%02x ", 
  233.              (uint)pp & 0xffff,
  234.              ((r_is_packed(pp)) ? '*' : ':'),
  235.              r_type(&temp));
  236.         debug_dump_one_ref(&temp);
  237.         dputc ('\n');
  238.        }
  239. }
  240.